package org.abratuhi.kvngroupware.controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import org.abratuhi.kvngroupware.client.KVNGroupwareClient;
import org.abratuhi.kvngroupware.gui.GUILogin;
import org.abratuhi.kvngroupware.network.Message;
import org.jdom.Element;

public class LoginController implements ActionListener, WindowListener {
	
	KVNGroupwareClient kvnclient;
	GUILogin guilogin;
	
	public LoginController(KVNGroupwareClient kvnclient, GUILogin guilogin){
		this.kvnclient = kvnclient;
		this.guilogin = guilogin;
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource().equals(this.guilogin.buttonLogin)){
			String id = this.guilogin.getId();
			String password = this.guilogin.getPassword();
			
			this.kvnclient.getUser().assignTempId(id);
			
			String[] attrNames = new String[]{"type", "action", "userId", "userPassword"};
			String[] attrValues= new String[]{"application", "login", id, password};
			String text = new String();
			Element[] content = new Element[0];
			
			Message loginMessage = this.kvnclient.createMessage(attrNames, attrValues, text, content);
			this.kvnclient.sendMessage(loginMessage);
		}
		else if(e.getSource().equals(this.guilogin.buttonRegistration)){
			this.kvnclient.getGUIRegistration().createJFrame();
			
			String[] attrNames = new String[]{"type", "action", "teamId", "seasonId", "leagueId"};
			String[] attrValues= new String[]{"application", "get_team", "", "", ""};
			String text = new String();
			Element[] content = new Element[0];
			Message getTeamsMessage = this.kvnclient.createMessage(attrNames, attrValues, text, content);
			this.kvnclient.sendMessage(getTeamsMessage);
		}
	}

	@Override
	public void windowActivated(WindowEvent e) {}

	@Override
	public void windowClosed(WindowEvent e) {}

	@Override
	public void windowClosing(WindowEvent e) {
		System.exit(0);
	}

	@Override
	public void windowDeactivated(WindowEvent e) {}

	@Override
	public void windowDeiconified(WindowEvent e) {}

	@Override
	public void windowIconified(WindowEvent e) {}

	@Override
	public void windowOpened(WindowEvent e) {}
}
